-- Coffin
-- Created by chengb Nov/10/2015
-- 圣棺格子

require "game/ui/form/dungeon/special/UICoffin"

-- 侍卫队长对应的冈布奥
local GUARDS_LEADER_PET_ID = 2;

-- 初始化
function initCoffin(self)
    self.item:removeAllChildren();

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.began then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 音效
            AudioM.playFx("ui_open1");

            local pos = self.gridData:getPos();
            local gridData = DungeonM.getGridByPos(pos);

            -- 如果在沼泽洞窟中
            if BraveVSDragonM.isInSwampCave() then
                local function okFunc()
                    if DungeonActionM.go("brave_building_bonus", self.gridData:getPos()) then
                        reviveGuardsLeader(self);
                    else
                        -- 没有相应道具
                        local buildingInfo = BuildingBonusM.queryBuildingView(self.gridData.class);
                        local failTip = buildingInfo["fail_tip"]
                        if type(failTip) == 'string' and string.len(failTip) > 0 then
                            alert(failTip);
                        end
                    end
                end

                self:openBuildingView(okFunc);
                return;
            end

            -- 打开圣棺界面
            UIMgr.getCurrentScene():removeFormByName("UICoffin");
            local uiForm = UICoffin.create(gridData);
            UIMgr.getCurrentScene():addForm(uiForm);
        end
    end

    local iconPath = getLevelItemIconPath("coffin2");
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.iconPath = iconPath;
    self.icon:setPosition(cc.p(2, 13));

    -- 图标初始隐藏
    if self.gridData.is_visible == 1 then
        self.icon:setVisible(true);
    else
        self.icon:setVisible(false);
    end
end

-- 领取圣棺奖励的回调
function onTakeCoffinBonus(self, bonus)
    local bonusInfo = {["bonus"] = bonus,};
    self:onBuildingBonus(bonusInfo);

    alert(getLocStr("take_coffin_bonus"));
end

-- 圣棺出现的回调
function onCoffinAppear(self)
    -- TODO 播放音效

    alert(getLocStr("coffin_appear_tip"));

    -- 播放出现动画
    performWithDelay(self, function() playCoffineAppear(self); end, 0.8);
end

-- 沼泽洞窟复活侍卫队长
function reviveGuardsLeader(self)
    startDialogueTemplate("guards_leader_talk2");
end